class Solution
{
public:
    vector<vector<int>> highestPeak(vector<vector<int>> &isWater)
    {
        int m = isWater.size();
        int n = isWater[0].size();
        vector<vector<int>> answer(m, vector<int>(n, 0));
        vector<vector<int>> visited(m, vector<int>(n, 0));
        vector<pair<int, int>> waterGrids;
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (isWater[i][j])
                {
                    waterGrids.push_back({i, j});
                    visited[i][j] = 1;
                }
            }
        }
        queue<pair<int, int>> q;
        for (auto &waterGrid : waterGrids)
        {
            q.push(waterGrid);
        }
        int nearHeight = 1;
        while (!q.empty())
        {
            int nearCount = q.size();
            for (int i = 0; i < nearCount; ++i)
            {
                auto [x, y] = q.front();
                q.pop();
                if (x > 0 && !visited[x - 1][y])
                {
                    visited[x - 1][y] = 1;
                    answer[x - 1][y] = nearHeight;
                    q.push({x - 1, y});
                }
                if (y > 0 && !visited[x][y - 1])
                {
                    visited[x][y - 1] = 1;
                    answer[x][y - 1] = nearHeight;
                    q.push({x, y - 1});
                }
                if (x < m - 1 && !visited[x + 1][y])
                {
                    visited[x + 1][y] = 1;
                    answer[x + 1][y] = nearHeight;
                    q.push({x + 1, y});
                }
                if (y < n - 1 && !visited[x][y + 1])
                {
                    visited[x][y + 1] = 1;
                    answer[x][y + 1] = nearHeight;
                    q.push({x, y + 1});
                }
            }
            ++nearHeight;
        }
        return answer;
    }
};